//
//  TimeTool.m
//  fastSign
//
//  Created by 朱潮 on 14-5-17.
//  Copyright (c) 2014年 zhuchao. All rights reserved.
//

#import "KLTimeTool.h"

const NSInteger KLSECOND = 1;
const NSInteger KLMINUTE = 60 * KLSECOND;
const NSInteger KLHOUR = 60 * KLMINUTE;
const NSInteger KLDAY = 24 * KLHOUR;
const NSInteger KLMONTH = 30 * KLDAY;
const NSInteger KLYEAR = 12 * KLMONTH;

@implementation KLTimeTool

KLDEF_SINGLETON(KLTimeTool);

+ (NSString *)getTimeDiffString:(NSTimeInterval)timestamp formatWith:(NSString *)format{
    timestamp = timestamp / 1000.0f;
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *todate = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDate *today = GET_TODAY_DATE; //当前时间
    unsigned int unitFlag = NSMonthCalendarUnit|NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *gap = [cal components:unitFlag fromDate:today toDate:todate options:0]; //计算时间差
   
    
    if(ABS([gap month])> 0){
        if (ABS([gap month])> 12) {
            return [NSString stringWithFormat:@"%ld年前", (long)(ABS([gap month] / 12))];
        }else{
            return [NSString stringWithFormat:@"%ld月前", (long)(ABS([gap month]))];
        }
    }
    else if (ABS([gap day]) > 0) {
        
        if (ABS([gap day]) == 1) {
            return [NSString stringWithFormat:@"%@", @"昨天"];
        }else if(ABS([gap day]) == 1){
            return [NSString stringWithFormat:@"%@", @"前天"];
        }else{
            return [NSString stringWithFormat:@"%ld天前", (long)(ABS([gap day]))];
        }
        
    }
    else if (ABS([gap hour]) > 0) {
        return [NSString stringWithFormat:@"%ld小时前", (long)(ABS([gap hour]))];
    }
    else {
        return [NSString stringWithFormat:@"%ld分钟前",  (long)(ABS([gap minute]))];
    }
}
+ (NSString *)getTimeDiffStringForMgr:(NSTimeInterval)timestamp formatWith:(NSString *)format{
    timestamp = timestamp / 1000.0f;
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *todate = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDate *today = GET_TODAY_DATE; //当前时间
    unsigned int unitFlag = NSMonthCalendarUnit|NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *gap = [cal components:unitFlag fromDate:today toDate:todate options:0]; //计算时间差
    
    if (ABS([gap day]) > 0) {
        return [self formatTime:timestamp formatWith:format];
    }
    else if (ABS([gap hour]) > 0) {
        return [NSString stringWithFormat:@"%ld小时前", (long)(ABS([gap hour]))];
    }
    else {
        NSInteger mintue = ABS([gap minute]);
        if (mintue < 1 ){
            return @"刚刚";
        }else{
            return [NSString stringWithFormat:@"%ld分钟前", (long)mintue];
        }

    }
}

+ (NSString *)getTimeDiffString:(NSTimeInterval)timestamp {
    return [self getTimeDiffString:timestamp formatWith:@"yyyy年MM月dd日"];
}
+(NSTimeInterval)formatTimeSinceNow:(NSTimeInterval)timestamp{
    NSDate *time = [NSDate dateWithTimeIntervalSinceNow:timestamp];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyy-MM-dd"];
	NSString *thisDayMin = [dateFormatter stringFromDate:time];
    
    return [[dateFormatter dateFromString:thisDayMin] timeIntervalSince1970];
}

+ (NSString *)formatDateSinceNow:(NSTimeInterval)timestamp formatWith:(NSString *)format{
    NSDate *time = [NSDate dateWithTimeIntervalSinceNow:timestamp];
    return [self formatDate:time formatWith:format];
}

+ (NSString *)fixStringForDate:(NSDate *)date formatWith:(NSString *)format{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    [dateFormatter setDateFormat:format];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"]];
    NSString *fixString = [dateFormatter stringFromDate:date];
    return fixString;
}

+ (NSString *)formatTime:(NSTimeInterval)timestamp formatWith:(NSString *)format{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    if(time == 0){
        return @"";
    }else{
        return [self fixStringForDate:confromTimesp formatWith:format];
    }
}

//format :yyyy-MM-dd
+ (NSString *)formatDate:(NSDate *)date formatWith:(NSString *)format{
//	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//	[dateFormatter setDateFormat:format];
//    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
//	NSString *currentDateStr = [dateFormatter stringFromDate:date];
//	return currentDateStr;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"]];
    NSString *thisDayMin = [dateFormatter stringFromDate:date];
    return thisDayMin;
}

/**
 *	获取时间的key 按星期  e.q "从 2013-08-05 至 2013-08-11"
 *
 *	@param	timestamp
 *
 *	@return
 */
+ (NSString *)getWeekKeyString:(NSTimeInterval)timestamp {
	NSTimeInterval timestampStart = [self getFirstDayOfWeek:timestamp];
	NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestampStart];
	NSString *timeStart = [self formatDate:date formatWith:@"yyyy-MM-dd"];
    
	NSString *timeEnd = [self formatDate:[date dateByAddingTimeInterval:3600 * 24 * 6] formatWith:@"yyyy-MM-dd"];
    
	return [NSString stringWithFormat:@"From %@ to %@", timeStart, timeEnd];
}

+ (NSString *)getFirstDayForWeekKeyString:(NSTimeInterval)timestamp {
	NSTimeInterval timestampStart = [self getFirstDayOfWeek:timestamp];
	NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestampStart];
	NSString *timeStart = [self formatDate:date formatWith:@"yyyy-MM-dd"];
    
	return timeStart;
}

+ (NSString *)getMonthKeyStringByOffset:(NSInteger)month {
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDateComponents *comps = [cal
	                           components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:GET_TODAY_DATE];
    
	comps.month = comps.month + month;
	if (comps.month > 12) {
		comps.year = comps.year + 1;
		comps.month =  comps.month - 12;
	}
	else if (comps.month < 1) {
		comps.year = comps.year - 1;
		comps.month = comps.month + 12;
	}
    
	NSDate *date = [cal dateFromComponents:comps];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy-MM"];
	NSString *currentDateStr = [dateFormatter stringFromDate:date];
	return currentDateStr;
}

+ (NSInteger)getTotalDayInMonth:(NSTimeInterval)timestamp{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate dateWithTimeIntervalSince1970:timestamp]];
    return range.length;
}


+ (NSTimeInterval)getFirstDayOfQuarter:(NSTimeInterval)timestamp {
	NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp];
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDateComponents *comps = [cal
	                           components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
	if (comps.month <= 3) {
		comps.month =  1;
	}
	else if (comps.month <= 6) {
		comps.month =  4;
	}
	else if (comps.month <= 9) {
		comps.month =  7;
	}
	else if (comps.month <= 12) {
		comps.month =  10;
	}
    
	comps.day = 1;
	NSDate *firstDay = [cal dateFromComponents:comps];
	return [firstDay timeIntervalSince1970];
}

+ (NSTimeInterval)getFirstDayOfWeek:(NSTimeInterval)timestamp {
	NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp];
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDateComponents *comps = [cal
	                           components:NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit
                               fromDate:now];
	//NSLog(@"%ld,%ld,%ld,%ld", (long)comps.year, (long)comps.month, (long)comps.week, (long)comps.weekday);
	if (comps.weekday < 2) {
		comps.week = comps.week - 1;
	}
	comps.weekday = 2;
	//   NSLog(@"%d,%d,%d,%d",comps.year,comps.month,comps.week,comps.weekday);
	NSDate *firstDay = [cal dateFromComponents:comps];
	return [firstDay timeIntervalSince1970];
}


+ (NSTimeInterval)getFirstDayOfMonth:(NSTimeInterval)timestamp {
	NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp];
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDateComponents *comps = [cal
	                           components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
	comps.day = 1;
	NSDate *firstDay = [cal dateFromComponents:comps];
	return [firstDay timeIntervalSince1970];
}

+ (NSTimeInterval)getFirstDayOfLastMonth:(NSTimeInterval)timestamp{
	NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp];
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDateComponents *comps = [cal
	                           components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    if(comps.month - 1 == 0 ){
        comps.month = 12;
        comps.year = comps.year -1;
    }else{
        comps.month = comps.month - 1;
    }
	comps.day = 1;
	NSDate *firstDay = [cal dateFromComponents:comps];
	return [firstDay timeIntervalSince1970];
}

//这个月
+ (NSUInteger)getSecond{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *today = GET_TODAY_DATE; //当前时间
    unsigned int unitFlag = NSSecondCalendarUnit;
    NSDateComponents *gap = [cal components:unitFlag fromDate:today]; //计算时间差
    
    return [gap month];
}

+ (NSUInteger)getMintue{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *today = GET_TODAY_DATE; //当前时间
    unsigned int unitFlag = NSMinuteCalendarUnit;
    NSDateComponents *gap = [cal components:unitFlag fromDate:today]; //计算时间差
    
    return [gap month];
}

+ (NSUInteger)getHour{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *today = GET_TODAY_DATE; //当前时间
    unsigned int unitFlag = NSHourCalendarUnit;
    NSDateComponents *gap = [cal components:unitFlag fromDate:today]; //计算时间差
    
    return [gap month];
}

+ (NSUInteger)getMonth{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *today = GET_TODAY_DATE; //当前时间
    unsigned int unitFlag = NSMonthCalendarUnit;
    NSDateComponents *gap = [cal components:unitFlag fromDate:today]; //计算时间差
    
    return [gap month];
}
+ (NSUInteger)getToday{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *today = GET_TODAY_DATE; //当前时间
    unsigned int unitFlag = NSDayCalendarUnit ;
    NSDateComponents *gap = [cal components:unitFlag fromDate:today]; //计算时间差
    
    return [gap day];
}
+ (NSUInteger)getYear{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *today = GET_TODAY_DATE; //当前时间
    unsigned int unitFlag = NSYearCalendarUnit;
    NSDateComponents *gap = [cal components:unitFlag fromDate:today]; //计算时间差
    
    return [gap year];
}

+ (NSDate*)dateFromYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%04d-%02d-%02x %02d:%02d:%02d",year,month,day,hour,minute,second]];
    
    return date;
}

+ (NSTimeInterval)timestampFromDateString:(NSString*)dateString formate:(NSString*)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:format];
    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"]];
    [formatter setDateFormat:format];
    NSDate *date = [formatter dateFromString:dateString];
    
    return [date timeIntervalSince1970];
}


+(NSDate*) convertDateFromString:(NSString*)uiDate   formate:(NSString*)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:format];
    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"]];
    [formatter setDateFormat:format];
    NSDate *date=[formatter dateFromString:uiDate];
    return date;
}

@end
